package com.cmc.cloud.cmclink.doc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.api.document.emums.RejectReasonEnum;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.RejectReasonErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.RejectReasonDO;
import com.cmc.cloud.cmclink.doc.mapper.RejectReasonMapper;
import com.cmc.cloud.cmclink.doc.service.RejectReasonService;
import com.cmc.cloud.cmclink.doc.vo.rejectreasonvo.RejectReasonCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.rejectreasonvo.RejectReasonPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.rejectreasonvo.RejectReasonUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

import cn.hutool.core.bean.BeanUtil;

/**
 * 常用订舱拒绝理由 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class RejectReasonServiceImpl implements RejectReasonService {

    @Resource
    private RejectReasonMapper rejectReasonMapper;

    @Override
    public void createRejectReason(RejectReasonCreateReqVO createReqVO) {
        // 插入
        RejectReasonDO rejectReason = BeanUtil.copyProperties(createReqVO, RejectReasonDO.class);
        rejectReasonMapper.insert(rejectReason);
    }

    @Override
    public void updateRejectReason(RejectReasonUpdateReqVO updateReqVO) {
        // 校验存在
        validateRejectReasonExists(updateReqVO.getId());
        // 更新
        RejectReasonDO updateObj = BeanUtil.copyProperties(updateReqVO, RejectReasonDO.class);
        rejectReasonMapper.updateById(updateObj);
    }

    @Override
    public void deleteRejectReason(Long id) {
        // 校验存在
        validateRejectReasonExists(id);
        // 删除
        rejectReasonMapper.deleteById(id);
    }

    private void validateRejectReasonExists(Long id) {
        if (rejectReasonMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(RejectReasonErrorCodeConstants.REJECT_REASON_NOT_EXISTS);
        }
    }

    @Override
    public RejectReasonDO getRejectReason(Long id) {
        return rejectReasonMapper.selectById(id);
    }

    @Override
    public List<RejectReasonDO> getRejectReasonList() {
        return rejectReasonMapper.selectList();
    }

    @Override
    public List<RejectReasonDO> getRejectReasonEnableList(Boolean enable) {
        LambdaQueryWrapperX<RejectReasonDO> queryWrapperX =
            new LambdaQueryWrapperX<RejectReasonDO>().eqIfPresent(RejectReasonDO::getEnable, enable)
                .eqIfPresent(RejectReasonDO::getReasonType, RejectReasonEnum.REJECT_REASON.getValue());
        return rejectReasonMapper.selectList(queryWrapperX);
    }

    @Override
    public PageResult<RejectReasonDO> getRejectReasonPage(RejectReasonPageReqVO reqVO) {
        LambdaQueryWrapperX<RejectReasonDO> queryWrapperX =
            new LambdaQueryWrapperX<RejectReasonDO>().eqIfPresent(RejectReasonDO::getReasonType, reqVO.getReasonType());
        return rejectReasonMapper.selectPage(reqVO, queryWrapperX);
    }
}
